package com.easy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easy.bean.Cart;
import com.easy.bean.Product;
import com.easy.bean.ProductImage;
import com.easy.common.CommonResult;
import com.easy.dao.ICartDao;
import com.easy.dao.IImageDao;
import com.easy.dao.IProductDao;
import com.easy.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Primary
public class CartImpl implements ICartService{
    @Autowired
    private ICartDao iCartDao;
    @Autowired
    private IImageDao iImageDao;
    @Autowired
    private IProductDao iProductDao;


    // 查询商品id  将商品信息添加进购物车
    @Override
    public CommonResult addcart(Cart cart) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("id",cart.getProductId());
        Product product = iProductDao.selectOne(wrapper);
        cart.setImage(product.getImage());
        cart.setAddPrice(product.getPrice());
        cart.setNum(1);
        cart.setDate(LocalDateTime.now());
        int result = iCartDao.insert(cart);
        if (result > 0) {
            return CommonResult.success("购物车添加商品成功");
        } else {
            return CommonResult.fail("购物车添加商品失败");
        }
    }
}
